package parser;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.MySpaceModel;
import org.xml.sax.Attributes;
import util.ParserHandler;
import org.apache.commons.lang.StringEscapeUtils;

public class MySpaceParser extends ParserHandler {

    ArrayList postList;
    private MySpaceModel post;
    private String parsedValue;

    public void parseMySpace(String file) throws SQLException {
        postList = new ArrayList<MySpaceModel>();
        if (parseDocument(file)) {
            System.out.println("\tParsed Document Successfully");
            printDocument();
            addData();
        } else {
            System.out.println("\tError while parsing document.");
        }
    }

    @Override
    public void startElement(String s, String s1, String elementName, Attributes attributes) {

        if (elementName.equalsIgnoreCase("item")) {
            try {
                post = new MySpaceModel();
                postList.add(post);
                post.setContainerList(postList);
            } catch (SQLException ex) {
                Logger.getLogger(MySpaceParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void characters(char ch[],
            int start,
            int length) {
        parsedValue = new String(ch, start, length);
    }

    @Override
    public void endElement(String namespaceURI,
            String localName,
            String elementName) {
        if (elementName.equalsIgnoreCase("title")) {
            String tempTitle = StringEscapeUtils.unescapeXml(parsedValue);
            post.setTitle(tempTitle);
        } else if (elementName.equalsIgnoreCase("pubDate")) {
            post.setCreated(parsedValue);
        } else if (elementName.equalsIgnoreCase("dc:source")) {
            post.setUserLink(parsedValue);
        } else if (elementName.equalsIgnoreCase("atom:name")) {
            String tempName = StringEscapeUtils.unescapeXml(parsedValue);
            post.setUser(tempName);
        } else if (elementName.equalsIgnoreCase("weblog:tier")) {
            post.setUserTier(Integer.parseInt(parsedValue));
        } else if (elementName.equalsIgnoreCase("description")) {
            post.setPost(parsedValue);
        }
    }

    private void addData() throws SQLException {
        System.out.println("Adding data....");
        post.addPost();
        System.out.println("Finish adding data......");
    }

    private void printDocument() {
        for (MySpaceModel i : post.getContainerList()) {
           /* String userSize = i.getUser().toString();
            if (userSize.length() < 0) {
                post.setUser("Guest");
            } else {
                System.out.println("User: " + i.getUser().replaceAll("\\s", " "));
            }*/
            System.out.println("Title: " + i.getTitle().replaceAll("\\s", " "));
            System.out.println("Posts: " + i.getPost().replaceAll("\\s", " "));
        }
    }
}
